package com.example.locationtracking.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.example.locationtracking.model.User;
import com.example.locationtracking.model.UserLocation;
import com.example.locationtracking.service.Service;
import com.example.locationtracking.service.impl.ServiceFactory;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class LTUtils {
	public static boolean checkLocationService(Context context){
		LocationManager lm  = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		boolean gps_enabled = false, network_enabled = false;		
		try {
			gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
		} catch (Exception ex) {
		}
		try {
			network_enabled = lm
					.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		} catch (Exception ex) {
		}

		if (!gps_enabled && !network_enabled) {
			return false;
		}
		return true;
	}
	public static boolean checkGPSisEnable(Context context){
		LocationManager lm  = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		boolean gps_enabled = false;		
		try {
			gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
		} catch (Exception ex) {
		}
		return gps_enabled;
	}
	public static boolean checkInternetConnection(Context context){
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		  NetworkInfo ni = cm.getActiveNetworkInfo();
		  if (ni == null) {
		   // There are no active networks.
		   return false;
		  } else
		   return true;
	}
	public static void saveValue(Context context, String key, String value){
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = settings.edit();
		editor.putString(key, value);
		editor.apply();
	}
	public static void saveIntValue(Context context, String key, int value){
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = settings.edit();
		editor.putInt(key, value);
		editor.apply();
	}
	public static String getValue(Context context, String key){
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
		return settings.getString(key, "");
	}
	private static void sendLocation(final Context context, final Location arg){
		Thread thread = new Thread(new Runnable() {

			@SuppressLint("SimpleDateFormat")
			@Override
			public void run() {
				
				Service service = ServiceFactory.getInstance()
						.getService();
				try {
					UserLocation userLocation = new UserLocation();
					userLocation.setUsername(LTUtils.getValue(context, Const.USER_NAME_KEY));
					//userLocation.setUsername("ggg");
					userLocation.setLatitude(String.valueOf(arg.getLatitude()));
					userLocation.setLongitude(String.valueOf(arg.getLongitude()));
					DateFormat df = new SimpleDateFormat(Const.DATE_FORMAT);
					Date date =new Date();
					userLocation.setTime(df.format(date));
					service.addLocation(userLocation);
					
				} catch(Exception e){
					e.printStackTrace();
					
				}
				
			}
		});
		Toast.makeText(context, "Update location: "+ arg.getLatitude() + "_" + arg.getLongitude(), Toast.LENGTH_SHORT).show();
		thread.start();
	}
	public static void updateLocation(Context context, Location arg0){
		String readData = LTUtils.getValue(context, Const.LA_LONG);
		if (readData.isEmpty()){
			//Toast.makeText(context, "Latitude1: "+ arg0.getLatitude() + " Longitude : " + arg0.getLongitude(), Toast.LENGTH_SHORT).show();
			String data = "" + String .valueOf(arg0.getLatitude()) + ":" + String .valueOf(arg0.getLongitude());
			LTUtils.saveValue(context, Const.LA_LONG, data);
			LTUtils.sendLocation(context, arg0);
		} else {
			String[] la_long = readData.split(":");
			Location location = new Location("Test");
			  location.setLatitude(Double.parseDouble(la_long[0]));
			  location.setLongitude(Double.parseDouble(la_long[1]));
			  location.setTime(new Date().getTime());
			  Log.d("Distance of current and new Location ", "" + arg0.distanceTo(location));
			if (arg0.distanceTo(location)>Const.DISTANCE){
				String data = "" + arg0.getLatitude() + ":" + arg0.getLongitude();
				LTUtils.saveValue(context, Const.LA_LONG, data);
				LTUtils.sendLocation(context, arg0);
			}
		}
	}
}
